home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / novice.el.z / novice.el
Encoding:
Text File  |  1998-05-21  |  5.5 KB  |  168 lines

  1. ;;; novice.el --- handling of disabled commands ("novice mode") for XEmacs.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994
  4. ;; Free Software Foundation, Inc.
  5.  
  6. ;; Maintainer: FSF
  7. ;; Keywords: internal, help
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; This mode provides a hook which is, by default, attached to various
  31. ;; putatively dangerous commands in a (probably futile) attempt to
  32. ;; prevent lusers from shooting themselves in the feet.
  33.  
  34. ;;; Code:
  35.  
  36. ;; This function is called (by autoloading)
  37. ;; to handle any disabled command.
  38. ;; The command is found in this-command
  39. ;; and the keys are returned by (this-command-keys).
  40.  
  41. ;;;###autoload
  42. ;(setq disabled-command-hook 'disabled-command-hook)
  43.  
  44. ;;;###autoload
  45. (defun disabled-command-hook (&rest ignore)
  46.   (let (char)
  47.     (save-window-excursion
  48.      (with-output-to-temp-buffer "*Help*"
  49.        (let ((keys (this-command-keys)))
  50.      (if (or (equal keys []) ;XEmacs kludge
  51.          (eq (event-to-character (aref keys 0)) ?\r))
  52.          (princ "You have invoked the disabled command ")
  53.        (princ "You have typed ")
  54.        (princ (key-description keys))
  55.        (princ ", invoking disabled command ")))
  56.        (princ this-command)
  57.        (princ ":\n")
  58.        ;; Print any special message saying why the command is disabled.
  59.        (if (stringp (get this-command 'disabled))
  60.        (princ (get this-command 'disabled)))
  61.        (princ (or (condition-case ()
  62.               (documentation this-command)
  63.             (error nil))
  64.           "<< not documented >>"))
  65.        ;; Keep only the first paragraph of the documentation.
  66.        (save-excursion
  67.      (set-buffer "*Help*")
  68.      (goto-char (point-min))
  69.      (if (search-forward "\n\n" nil t)
  70.          (delete-region (1- (point)) (point-max))
  71.        (goto-char (point-max))))
  72.        (princ "\n\n")
  73.        (princ "You can now type
  74. Space to try the command just this once,
  75.       but leave it disabled,
  76. Y to try it and enable it (no questions if you use it again),
  77. N to do nothing (command remains disabled).")
  78.        (save-excursion
  79.     (set-buffer standard-output)
  80.     (help-mode)))
  81.      (message "Type y, n or Space: ")
  82. ;     (let ((cursor-in-echo-area t))
  83. ;       (while (not (memq (setq char (downcase (read-char)))
  84. ;             '(?  ?y ?n)))
  85. ;     (ding)
  86. ;     (message "Please type y, n or Space: "))))
  87.      ;; XEmacs version
  88.      (let ((cursor-in-echo-area t)
  89.        (inhibit-quit t)
  90.        event)
  91.        (while (null char)
  92.      (if (progn
  93.            (setq event (next-command-event))
  94.            (prog1
  95.            (or quit-flag (eq 'keyboard-quit (key-binding event)))
  96.          (setq quit-flag nil)))
  97.          (progn
  98.            (setq quit-flag nil)
  99.            (signal 'quit '())))
  100.      (let* ((key (and (key-press-event-p event) (event-key event)))
  101.         (rchar (and key (event-to-character event))))
  102.        (if rchar (setq rchar (downcase rchar)))
  103.        (cond ((eq rchar ?y)
  104.           (setq char rchar))
  105.          ((eq rchar ?n)
  106.           (setq char rchar))
  107.          ((eq rchar ? )
  108.           (setq char rchar))
  109.          (t
  110.           (ding nil 'y-or-n-p)
  111.           (discard-input)
  112.           (message "Please type y, n or Space: ")))))))
  113.     (message nil)
  114.     (if (= char ?y)
  115.     (if (and user-init-file
  116.          (not (string= "" user-init-file))
  117.          (y-or-n-p "Enable command for future editing sessions also? "))
  118.         (enable-command this-command)
  119.       (put this-command 'disabled nil)))
  120.     (if (/= char ?n)
  121.     (call-interactively this-command))))
  122.  
  123. ;;;###autoload
  124. (defun enable-command (command)
  125.   "Allow COMMAND to be executed without special confirmation from now on.
  126. The user's `custom-file' is altered so that this will apply
  127. to future sessions."
  128.   (interactive "CEnable command: ")
  129.   (put command 'disabled nil)
  130.   (save-excursion
  131.    (set-buffer (find-file-noselect
  132.         (substitute-in-file-name custom-file)))
  133.    (goto-char (point-min))
  134.    (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
  135.        (delete-region
  136.     (progn (beginning-of-line) (point))
  137.     (progn (forward-line 1) (point))))
  138.    ;; Explicitly enable, in case this command is disabled by default
  139.    ;; or in case the code we deleted was actually a comment.
  140.    (goto-char (point-max))
  141.    (or (bolp) (insert "\n"))
  142.    (insert "(put '" (symbol-name command) " 'disabled nil)\n")
  143.    (save-buffer)))
  144.  
  145. ;;;###autoload
  146. (defun disable-command (command)
  147.   "Require special confirmation to execute COMMAND from now on.
  148. The user's `custom-file' is altered so that this will apply
  149. to future sessions."
  150.   (interactive "CDisable command: ")
  151.   (if (not (commandp command))
  152.       (error "Invalid command name `%s'" command))
  153.   (put command 'disabled t)
  154.   (save-excursion
  155.    (set-buffer (find-file-noselect
  156.         (substitute-in-file-name custom-file)))
  157.    (goto-char (point-min))
  158.    (if (search-forward (concat "(put '" (symbol-name command) " ") nil t)
  159.        (delete-region
  160.     (progn (beginning-of-line) (point))
  161.     (progn (forward-line 1) (point))))
  162.    (goto-char (point-max))
  163.    (or (bolp) (insert "\n"))
  164.    (insert "(put '" (symbol-name command) " 'disabled t)\n")
  165.    (save-buffer)))
  166.  
  167. ;;; novice.el ends here
  168.